Why is a NullReferenceException thrown when a ToolStrip button is clicked twice with code in the `Click` event handler?
Posted
by
Patrick
on Stack Overflow
See other posts from Stack Overflow
or by Patrick
Published on 2011-01-02T02:19:39Z
Indexed on
2011/01/02
4:53 UTC
Read the original article
Hit count: 292
I created a clean WindowsFormsApplication solution, added a ToolStrip
to the main form, and placed one button on it. I've added also an OpenFileDialog
, so that the Click
event of the ToolStripButton
looks like the following:
private void toolStripButton1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
}
I didn't change any other properties or events.
The funny thing is that when I double-click the ToolStripButton
(the second click must be quite fast, before the dialog opens), then cancel both dialogs (or choose a file, it doesn't really matter) and then click in the client area of main form, a NullReferenceException
crashes the application (error details attached at the end of the post). Please note that the Click
event is implemented while DoubleClick
is not.
What's even more strange that when the OpenFileDialog
is replaced by any user-implemented form, the ToolStripButton
blocks from being clicked twice.
I'm using VS2008 with .NET3.5. I didn't change many options in VS (only fontsize, workspace folder and line numbering).
Does anyone know how to solve this? It is 100% replicable on my machine, is it on others too?
One solution that I can think of is disabling the button before calling OpenFileDialog.ShowDialog()
and then enabling the button back (but it's not nice). Any other ideas?
And now the promised error details:
System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.NativeWindow.WindowClass.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.PeekMessage(MSG& msg, HandleRef hwnd, Int32 msgMin, Int32 msgMax, Int32 remove)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsFormsApplication1.Program.Main() w C:\Users\Marchewek\Desktop\Workspaces\VisualStudio\WindowsFormsApplication1\Program.cs:line 20
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
© Stack Overflow or respective owner